
<script setup>
import { ref } from 'vue';
import { ElMessage } from 'element-plus';

const form = ref({
  username: '',
  password: '',
  confirmPassword: '',
  age: '',
  gender: '',
  hobbies: [],
  occupation: '',
  introduction: '',
});
const user = ref(null);
const dialogTableVisible = ref(false);

const validatePassword = (rule, value, callback) => {
  if (value.length < 6) {
    callback(new Error('密码长度不能少于6个字符'));
  } else if (!/\d/.test(value)) {
    callback(new Error('密码必须包含至少一个数字'));
  } else if (!/[a-zA-Z]/.test(value)) {
    callback(new Error('密码必须包含至少一个字母'));
  } else {
    callback();
  }
};

const validateConfirmPassword = (rule, value, callback) => {
  if (value !== form.value.password) {
    callback(new Error('两次输入的密码不一致'));
  } else {
    callback();
  }
};

const validateAge = (rule, value, callback) => {
  if (value < 0 || value > 150) {
    callback(new Error('请输入有效的年龄'));
  } else {
    callback();
  }
};

const onSubmit = () => {
  const formRef = ref(null);
  formRef.value.validate((valid) => {
    if (valid) {
      ElMessage.success('注册成功！');
      user.value = {
        name: form.value.username,
        age: parseInt(form.value.age),
        address: form.value.address,
      };
      console.log('注册信息：', form.value);
      dialogTableVisible.value = true;
    } else {
      ElMessage.error('请检查输入信息！');
      return false;
    }
  });
};
</script>

<template>
  <el-row>
    <el-col :md="24" :lg="8"></el-col>
    <el-col :md="24" :lg="8">
      <el-form :model="form" ref="form" :label-width="auto" style="max-width: 600px">
        <el-form-item label="用户名" prop="username" :rules="[{ required: true, message: '请输入用户名', trigger: 'blur' }, { min: 4, message: '用户名长度不能少于4个字符', trigger: 'blur' }]">
          <el-input v-model="form.username" />
        </el-form-item>
        <el-form-item label="密码" prop="password" :rules="[{ required: true, message: '请输入密码', trigger: 'blur' }, { min: 6, message: '密码长度不能少于6个字符', trigger: 'blur' }, { validator: validatePassword, trigger: 'blur' }]">
          <el-input v-model="form.password" type="password" />
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmPassword" :rules="[{ required: true, message: '请再次输入密码', trigger: 'blur' }, { validator: validateConfirmPassword, trigger: 'blur' }]">
          <el-input v-model="form.confirmPassword" type="password" />
        </el-form-item>
        <el-form-item label="年龄" prop="age" :rules="[{ required: true, message: '请输入年龄', trigger: 'blur' }, { validator: validateAge, trigger: 'blur' }]">
          <el-input v-model="form.age" type="number" />
        </el-form-item>
        <el-form-item label="性别" prop="gender" :rules="[{ required: true, message: '请选择性别', trigger: 'change' }]">
          <el-radio-group v-model="form.gender">
            <el-radio label="男">男</el-radio>
            <el-radio label="女">女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="兴趣爱好">
          <el-checkbox-group v-model="form.hobbies">
            <el-checkbox label="阅读"></el-checkbox>
            <el-checkbox label="运动"></el-checkbox>
            <el-checkbox label="旅行"></el-checkbox>
            <el-checkbox label="数码"></el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="职业" prop="occupation" :rules="[{ required: true, message: '请选择职业', trigger: 'change' }]">
          <el-select v-model="form.occupation" placeholder="请选择">
            <el-option label="前端开发工程师" value="前端开发工程师"></el-option>
            <el-option label="后端开发工程师" value="后端开发工程师"></el-option>
            <el-option label="设计师" value="设计师"></el-option>
            <el-option label="产品经理" value="产品经理"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="自我介绍">
          <el-input type="textarea" v-model="form.introduction"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">提交</el-button>
          <el-button>取消</el-button>
        </el-form-item>
      </el-form>
      <div v-if="user">
        <p>用户信息: {{ user }}</p>
        <p v-if="user.name && (user.age >= 18)">成年</p>
        <p v-else>未成年</p>
        <p>{{ form.age }}</p>
        <p>{{ form.gender }}</p>
        <p>{{ form.hobbies.join(', ') }}</p>
        <p>{{ form.occupation }}</p>
        <p>{{ form.introduction }}</p>
      </div>
    </el-col>
    <el-col :md="24" :lg="8"></el-col>
  </el-row>
  <el-dialog v-model="dialogTableVisible" title="请确认注册信息" width="500">
    <p>{{ form.username }}</p>
    <p>{{ form.age }}</p>
    <p>{{ form.gender }}</p>
    <p>{{ form.hobbies.join(', ') }}</p>
    <p>{{ form.occupation }}</p>
    <p>{{ form.introduction }}</p>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogTableVisible = false">取消</el-button>
        <el-button type="primary" @click="dialogTableVisible = false">提交</el-button>
      </div>
    </template>
  </el-dialog>
</template>